<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-11-22 17:16
 *
 * Version: 2.0
 */

namespace App\Modules\Coupon\Repositories;

use App\Exceptions\AllValidatorException;
use App\Exceptions\NotFoundException;
use App\Models\Coupon;
use Flower\Core\Repository\Eloquent\BaseRepository;

class CouponRepository extends BaseRepository
{
	public function model()
	{
		return Coupon::class;
	}

//	public function validator()
//	{
//		return parent::validator(); // TODO: Change the autogenerated stub
//	}

	public function add(array $params)
	{
		$data = [
			'store_id' => $params['store_id'],
			'user_id' => $params['user_id'],
			'name' => $params['name'],
			'type' => $params['type'],
			'money' => isset($params['money']) ? $params['money'] : 0,
			'discount' => isset($params['discount']) ? $params['discount']*10 : 0,
			'use_condition' => isset($params['use_condition']) ? $params['use_condition'] : 0,
			'createnum' => $params['createnum'],
			'use_start_time' => date('Y-m-d H:i:s',strtotime($params['use_start_time'])),
			'use_end_time' => date('Y-m-d H:i:s',strtotime($params['use_end_time'])),
			'send_start_time' => date('Y-m-d H:i:s'),
			'send_end_time' => date('Y-m-d H:i:s',strtotime('+1 month')),
			'status' => 1
		];
		return $this->create($data);
	}

	/**
	 * 检测是否存在
	 * @param $id
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function IsExisted($id){
		$info = $this->find($id);
		if(empty($info)){
			throw new NotFoundException();
		}
		return $info;
	}


	/**
	 * 检测是否被领取
	 * @param $id
	 * @param $info
	 * @return mixed
	 * @throws AllValidatorException
	 * @throws NotFoundException
	 */
	public function isReceivedByUserOrMember($id,$infoMsg="编辑"){
		$info = $this->IsExisted($id);
		if($info->send_num){
			throw new AllValidatorException(['msg'=>'该优惠券已经被领取，不能被'.$infoMsg]);
		}
		return $info;
	}




	/**
	 * 发布
	 * @param $id
	 * @return bool
	 * @throws NotFoundException
	 */
	public function post($id){
		$info = $this->IsExisted($id);
		$info->status = 1;
		$info->save();
		return true;
	}

	/**
	 * 删除优惠券
	 * @param $id
	 * @return int
	 * @throws NotFoundException
	 */
	public function del($id){
		$info = $this->IsExisted($id);
		if($info->status){
			throw new NotFoundException(['msg'=>'优惠券已经发布，不能删除！']);
		}
		return $this->delete($id);
	}

	/**
	 * 编辑优惠券
	 * @param $params
	 * @return mixed
	 */
	public function edit($id,$params){
		$params['discount'] = isset($params['discount']) ? $params['discount']*10 : 0;
		$params['use_condition'] = isset($params['use_condition']) ? $params['use_condition'] : 0;
		return $this->update($params,$id);
	}


	/**
	 * 优惠券列表
	 * @param $storeId
	 * @return mixed
	 */
	public function getCouponListToMember($storeId,$type=0,$is_live=1,$pageSize=10){
		$list =$this->model->where(['status'=>1,'store_id'=>$storeId])->when(!empty($type),function ($v) use($type){
			$v->where(['type'=>$type]);
		})->where('createnum','>','send_num')->whereDate('send_end_time','>=',date('Y-m-d'))->orderBy('id','desc');
		return $is_live ? $list->get()->toArray() : $list->paginate($pageSize, ['*'], 'paginate');
	}


	/**
	 * 更新优惠券数据
	 * @param $couponId
	 * @return array
	 * @throws NotFoundException
	 */
	public function updateSendNums($couponId){
		$coupon = $this->IsExisted($couponId);
		if($coupon->createnum > $coupon->send_num){
			$coupon->send_num += 1;
			$coupon->save();
			return ['status'=>1,'data'=>$couponId];
		}
		return ['status'=>0,'data'=>$couponId];
	}


	/**
	 * 回收操作
	 * @param $couponId
	 * @param $num
	 * @return bool
	 */
	public function backCoupon($couponId,$num){
		$coupon = $this->find($couponId);
		$coupon->send_num += $num;
		$coupon->save();
		return true;
	}
}